import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/7/24 17:53
 * @version:v1.0
 */
public class testSearch {
//[3,2,4]
//    6

    public static void main(String[] args) {
        int[] nums ={4,2,6};

        int key = 6;
        commonBinarySearch(nums,key);

    }

    public static int commonBinarySearch(int[] nums, int target) {
        int[][] ans = new int[nums.length][2];
        for(int i =0;i<nums.length;i++){
            ans[i][0]=nums[i];
            ans[i][1]=i;
        }
        for (int[] num:ans
        ) {
            System.out.println(Arrays.toString(num));
        }

        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        int low = 0;
        int high = nums.length - 1;
        int middle = 0;      //定义middle
        int key =0;
        for(int i =0;i<nums.length-1;i++) {
            low = i+1;
            high = nums.length - 1;
            middle = 0;
            key=target-nums[i];
            if(key>nums[nums.length - 1]) continue;
            if (key < nums[low] || key > nums[high] || low > high) {
                return -1;
            }

            while (low <= high) {
                middle = (low + high) / 2;
                if (nums[middle] > key) {
                    high = middle - 1;
                } else if (nums[middle] < key) {
                    low = middle + 1;
                } else {
                    System.out.println(i);
                    System.out.println(middle);
                    return middle;
                }
            }
        }
        return -1;    //最后仍然没有找到，则返回-1
    }

}

/**
 * 不使用递归的二分查找
 * title:commonBinarySearch
 *
 * @param nums
 * @param key
 * @return 关键字位置
 */
